<?php
/**
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2023/2/24
 * Time: 10:55
 */

namespace Util\Movie;

use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Factory;

class Tuzyp
{
    private $appid;

    private $appSecret;

    private $url = 'https://test.tuzyp.com/';

    // access_token redis缓存key
    private $accessTokenKey = 'movieTuzypAccessTokenKey';

    // cityId redis缓存key
    private $cityIdKey = 'movieTuzypCityIdKeyKey';

    public function __construct($appid, $appSecret)
    {
        $this->appid = $appid;
        $this->appSecret = $appSecret;
    }

    /**
     * 影院列表
     * https://www.showdoc.com.cn/1481093539997164/7186397295832191
     */
    public function cinema($cityId)
    {
        $accessToken = self::accessToken();
        if(!$accessToken->isSuccess()){
            return ResultWrapper::fail($accessToken->getData(), $accessToken->getErrorCode());
        }
        $accessToken = $accessToken->getData();

        $apiUrl = $this->url.'api/movie/info/get-cinema-list';

        $postData = [
            'appId' => $this->appid,
            'access_token' => $accessToken,
        ];

        if($cityId){
            $postData['cityId'] = $cityId;
        }

        $response = request($apiUrl, $postData, 30);

        $response = self::commonResult($response);
        if(!$response->isSuccess()){
            return ResultWrapper::fail($response->getData(), $response->getErrorCode());
        }
        $response = $response->getData();

        return ResultWrapper::success($response);
    }

    /**
     * 即将上映
     * https://www.showdoc.com.cn/1481093539997164/7186361640877634
     */
    public function soon($cityId)
    {
        $accessToken = self::accessToken();
        if(!$accessToken->isSuccess()){
            return ResultWrapper::fail($accessToken->getData(), $accessToken->getErrorCode());
        }
        $accessToken = $accessToken->getData();

        $apiUrl = $this->url.'api/movie/info/get-soon-list';

        $postData = [
            'appId' => $this->appid,
            'access_token' => $accessToken,
        ];

        if($cityId){
            $postData['cityId'] = $cityId;
        }

        $response = request($apiUrl, $postData, 30);

        $response = self::commonResult($response);
        if(!$response->isSuccess()){
            return ResultWrapper::fail($response->getData(), $response->getErrorCode());
        }
        $response = $response->getData();

        return ResultWrapper::success($response);
    }


    /**
     * 获取正在热映列表
     * https://www.showdoc.com.cn/1481093539997164/7186303997343174
     */
    public function getHotList($cityId)
    {
        $accessToken = self::accessToken();
        if(!$accessToken->isSuccess()){
            return ResultWrapper::fail($accessToken->getData(), $accessToken->getErrorCode());
        }
        $accessToken = $accessToken->getData();

        $apiUrl = $this->url.'api/movie/info/get-hot-list';

        $postData = [
            'appId' => $this->appid,
            'access_token' => $accessToken,
        ];

        if($cityId){
            $postData['cityId'] = $cityId;
        }

        $response = request($apiUrl, $postData, 30);

        $response = self::commonResult($response);
        if(!$response->isSuccess()){
            return ResultWrapper::fail($response->getData(), $response->getErrorCode());
        }
        $response = $response->getData();

        return ResultWrapper::success($response);
    }

    /**
     * 城市下区域
     * https://www.showdoc.com.cn/1481093539997164/7186146101755682
     */
    public function getCityIdByArea($areaName, $cityId)
    {
        $cityId = Factory::cache('default')->hget($this->cityIdKey, md5($areaName));
        if (!empty($cityId)) {
            return ResultWrapper::success($cityId);
        }

        $accessToken = self::accessToken();
        if(!$accessToken->isSuccess()){
            return ResultWrapper::fail($accessToken->getData(), $accessToken->getErrorCode());
        }
        $accessToken = $accessToken->getData();

        $apiUrl = $this->url.'api/movie/info/get-city-area';

        $postData = [
            'appId' => $this->appid,
            'access_token' => $accessToken,
            'cityId'  => $cityId,
        ];

        $response = request($apiUrl, $postData, 30);

        $response = self::commonResult($response);
        if(!$response->isSuccess()){
            return ResultWrapper::fail($response->getData(), $response->getErrorCode());
        }
        $response = $response->getData();

        foreach($response as $key => $value){
            if($value['areaName'] == $areaName){
                Factory::cache('default')->hset($this->accessTokenKey, md5($areaName), $value['areaId']);
                return ResultWrapper::success($value['cityId']);
            }
        }

        return ResultWrapper::success('当前城市下区域查询不到id', ErrorCode::$apiNotResult);
    }


    /**
     * 获取城市id
     * https://www.showdoc.com.cn/1481093539997164/7185999157511417
     */
    public function getCityId($cityName)
    {
        $cityId = Factory::cache('default')->hget($this->cityIdKey, md5($cityName));
        if (!empty($cityId)) {
            return ResultWrapper::success($cityId);
        }

        $accessToken = self::accessToken();
        if(!$accessToken->isSuccess()){
            return ResultWrapper::fail($accessToken->getData(), $accessToken->getErrorCode());
        }
        $accessToken = $accessToken->getData();

        $apiUrl = $this->url.'api/movie/info/get-city-list';

        $postData = [
            'appId' => $this->appid,
            'access_token' => $accessToken,
        ];

        $response = request($apiUrl, $postData, 30);

        $response = self::commonResult($response);
        if(!$response->isSuccess()){
            return ResultWrapper::fail($response->getData(), $response->getErrorCode());
        }
        $response = $response->getData();

        foreach($response as $key => $value){
            if($value['regionName'] == $cityName){
                Factory::cache('default')->hset($this->accessTokenKey, md5($cityName), $value['cityId']);
                return ResultWrapper::success($value['cityId']);
            }
        }

        return ResultWrapper::success('当前城市查询不到id', ErrorCode::$apiNotResult);
    }

    /**
     * 生成签名方法
     * https://www.showdoc.com.cn/1481093539997164/7184595155212186
     */
    public function sign()
    {
        return md5($this->appid.time().$this->appSecret);
    }


    /**
     * 接口请求token
     * https://www.showdoc.com.cn/1481093539997164/7184595155212186
     */
    public function accessToken()
    {
        $accessToken = Factory::cache('default')->get($this->accessTokenKey);
        if (!empty($accessToken)) {
            return ResultWrapper::success($accessToken);
        }

        $sign = self::sign();

        $apiUrl = $this->url.'api/movie/auth/accesstoken';

        $postData = [
            'appId' => $this->appid,
            'timestamp' => time(),
            'sign'  => $sign
        ];

        $response = request($apiUrl, $postData, 30);

        $response = self::commonResult($response);
        if(!$response->isSuccess()){
            return ResultWrapper::fail($response->getData(), $response->getErrorCode());
        }
        $response = $response->getData();
        $accessToken = $response['access_token'];

        Factory::cache('default')->set($this->accessTokenKey, $accessToken, 7000);

        return ResultWrapper::success($accessToken);
    }



    /**
     * 公共的接口请求处理办法
     * @param $response
     * @return ResultWrapper
     */
    public function commonResult($response)
    {
        if ($response['httpcode'] != 200) {
            return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
        }

        $responseData = json_decode($response['content'], true);

        if($responseData['code'] != 200){
            return ResultWrapper::fail($responseData['msg'], ErrorCode::$apiNotResult);
        }

        return ResultWrapper::success($responseData['data']);
    }
}